package com.estate.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.estate.common.utils.DateUtils;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.system.domain.ResidentialDetail;
import com.estate.system.domain.Residential;
import com.estate.system.mapper.ResidentialDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.codec.cbor.Jackson2CborDecoder;
import org.springframework.stereotype.Service;
import com.estate.system.mapper.ResidentialMapper;
import com.estate.system.domain.Residential;
import com.estate.system.service.IResidentialService;
import com.estate.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author estate
 * @date 2022-04-08
 */
@Service
public class ResidentialServiceImpl implements IResidentialService
{
    @Autowired
    private ResidentialMapper residentialMapper;
    @Resource
    private ResidentialDetailMapper residentialDetailMapper;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Residential selectResidentialById(Long id)
    {
        Gson gson = new Gson();
        String residentialJson = (String)redisUtil.get("residential:" + id);
        //有可能存在缓存穿透的问题
        Residential residential = new Residential();
        if (residentialJson!=null && residentialJson.equals("noExist")){
            System.out.println("bothnoExist1");
            return null;
        }else if(residentialJson==null){
            residential=residentialMapper.selectResidentialById(id);
            if (residential==null){
                redisUtil.set("residential:"+id,"noExist");
                System.out.println("bothnoExist");
                return null;
            }else{
                String s = gson.toJson(residential);
                redisUtil.set("residential:"+id,s);
                return residential;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回
            Residential residential1 = gson.fromJson(residentialJson, Residential.class);

            return residential1;

        }
    }
    //一次性给出所有数据,是比较需要缓存,但是每次更新多少要删除这个所有数据
    /*
     * @onePageSize 一页多少数据
     * @pageNum   第几页
     * */
    @Override
    public PageInfo<Residential> selectAllResidentialList(int onePageSize, int pageNum) {




        Gson gson = new Gson();
        List allList = redisUtil.lGet("allResidential:",0,-1);

        //有可能存在缓存穿透的问题

        if (allList!=null && allList.size()==1 && allList.get(0)=="noExist" ){
            System.out.println("bothnoExist1");
            return null;
        }else if(allList.size()==0){

            List<Residential> residentialList1 = residentialMapper.selectAllResidentialList();
            System.out.println("residentialList1"+residentialList1);
            System.out.println("hasSelectAllResidentialList");
            if (residentialList1==null){
                redisUtil.lSet("allResidential:","noExist");
                System.out.println("bothnoExist");
                return null;
            }else{

                for (Residential residential : residentialList1) {
                    redisUtil.lSet("allResidential:",residential);
                }
                PageInfo<Residential> residentialPageInfo = redisResidentialPage(onePageSize, pageNum, residentialList1);


                return residentialPageInfo;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回,
            //分页后的结果,也是要知道分页的结果的....
            PageInfo<Residential> residentialPageInfo = redisResidentialPage(onePageSize, pageNum, allList);
            return residentialPageInfo;

        }

    }
    /**
     *
     * 不分页的redis查询
     *
     *
     * **/
    @Override
    public List<Residential> selectAllResidentialList() {


        Gson gson = new Gson();
        List allList = redisUtil.lGet("allResidential:",0,-1);

        //有可能存在缓存穿透的问题

        if (allList!=null && allList.size()==1 && allList.get(0)=="noExist" ){
            System.out.println("bothnoExist1");
            return null;
        }else if(allList.size()==0){

            List<Residential> residentialList1 = residentialMapper.selectAllResidentialList();
            System.out.println("residentialList1"+residentialList1);
            System.out.println("hasSelectAllResidentialList");
            if (residentialList1==null){
                redisUtil.lSet("allResidential:","noExist");
                System.out.println("bothnoExist");
                return null;
            }else{

                for (Residential residential : residentialList1) {
                    redisUtil.lSet("allResidential:",residential);
                }
                List objects = redisUtil.lGet("allResidential:",0,-1);


                return objects;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回,
            //分页后的结果

            return allList;

        }

    }
    @Override
    public List<Residential> selectAllResidentialList(Residential residential) {

        List<Residential> residentialList1 = residentialMapper.selectAllResidentialList();
        return residentialList1;

    }
    /*
     * 给redis里面数据分页
     *
     * */

    public PageInfo<Residential> redisResidentialPage(int onePageSize, int pageNum,List objects){
        //分页
        //数据条数
        int selectNum;
        //计算全部分多少页
        int pagesize;
        int start;
        int end;

        //先计算总长度,然后全部多少页,页码是从哪里到哪里
//        List<Object> objects = redisUtil.lGet("allResidential:", 0, -1);
        System.out.println(objects.size());
        //得到

        selectNum=objects.size();
        System.out.println("selectNum"+selectNum);
        //15/10===>1.5
        //得到分页页数
        if(selectNum%onePageSize==0){
            pagesize=selectNum/selectNum;

        }else{
            pagesize=(selectNum/selectNum)+1;

        }
        System.out.println(pagesize);
        if(pageNum==1){
            //得到当前第页的数据
            List objects1 = redisUtil.lGet("allResidential:", 0, onePageSize-1);
            System.out.println("onePageSize"+objects1.size());
            PageInfo<Residential> residentialPageInfo = new PageInfo<>();
            residentialPageInfo.setList(objects1);
            residentialPageInfo.setPages(pagesize);
            residentialPageInfo.setTotal(objects.size());
            System.out.println("objects.size()"+objects.size());
            return residentialPageInfo;
        }else {
            System.out.println("pageNum * onePageSize"+(pageNum-1) * onePageSize+",pageNum * onePageSize + onePageSize"+pageNum * onePageSize);
            List objects1 = redisUtil.lGet("allResidential:", (pageNum-1) * onePageSize, pageNum * onePageSize-1);
            PageInfo<Residential> residentialPageInfo = new PageInfo<>();
            residentialPageInfo.setList(objects1);
            System.out.println("objects1.size()"+objects1.size());
            residentialPageInfo.setPages(pagesize);
            residentialPageInfo.setTotal(objects.size());
            return residentialPageInfo;

        }


    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param residential 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Residential> selectResidentialList(Residential residential)
    {
        Gson gson = new Gson();
        String res = gson.toJson(residential);
        //如果redis有这个整个对象的数据,那么返回
        String redisString = (String)redisUtil.get(res);
        System.out.println(redisString);
        if(redisString!=null && redisString.equals("noExist")){
            return null;


        }else if(redisString!=null ){

            System.out.println("redisString");
            List<Residential> list= gson.fromJson(redisString, List.class);
            System.out.println("92"+list);
            return list;


        }else if (redisString==null){
            System.out.println("redisString==null");
            System.out.println(residential);

            List<Residential> list = residentialMapper.selectResidentialList(residential);
            System.out.println("list"+list);
            if (list.size()==0){
                redisUtil.set(res,"noExist");
                System.out.println("noExist");
                return null;
            }else{
                String redisJson = gson.toJson(list);
                redisUtil.set(res,redisJson);
                return list;
            }

        }
        //这个有点麻烦呀....需要判断residential全部的东西
        return null;
    }
    @Override
    public List<Residential> selectResidentialListByMapper(Residential residential) {
        return residentialMapper.selectResidentialList(residential);
    }

    public List<Residential> selectByTitleList(Residential residential)
    {
        return residentialMapper.selectByTitleList(residential);
    }



    /**
     * 新增residential表一条记录
     * 新增residentialdetail表一条同id的记录
     * 先插入到residential表,再查询residential表id
     * @param residential 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertResidential(Residential residential)
    {
        residential.setCreateTime(DateUtils.getNowDate());
        int res = residentialMapper.insertResidential(residential);
        if (res>0){
            redisUtil.del("allResidential:");
        }

        List<Residential> residentials = residentialMapper.selectResidentialList(residential);
        Residential residential1 = residentials.get(0);
        System.out.println(residential1);

        ResidentialDetail residentialDetail=new ResidentialDetail();
        residentialDetail.setId(residential.getId());
        residentialDetail.setDetailPic("[]");
        residentialDetail.setHeadpic("[]");
        residentialDetail.setVideoAddress("[]");
        System.out.println("residential1.getId()"+residential1.getId());


        return residentialDetailMapper.insertResidentialDetail(residentialDetail);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param residential 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateResidential(Residential residential)
    {
        System.out.println(residential);
        redisUtil.del("residential:"+residential.getId());
        redisUtil.del("allResidential:");
        return residentialMapper.updateResidential(residential);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteResidentialByIds(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        redisUtil.del("allResidential:");
        return residentialMapper.deleteResidentialByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteResidentialById(Long id)
    {
        int res = residentialMapper.deleteResidentialById(id);
        if (res>0){
            redisUtil.del("residential:"+id);
            redisUtil.del("allResidential:");
        }


        return res;
    }
}
